/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fase2.pt.iul.sid.database;

import java.sql.*;

/**
 *
 * @author Ricardo Marques
 */
public class FactoryConnection {
    
    private String db;
    private String user;
    private String passwd;
    private Connection con = null;
    
    public FactoryConnection(String db, String user, String passwd){
        
        this.db = db;
        this.user = user;
        this.passwd = passwd;
        createConnection();
        
    }
    
    private void createConnection(){
               
        String dbUrl = "jdbc:sqlanywhere:Tds:localhost:2638?eng="+db; 
 
        try 
        { 
            con = DriverManager.getConnection(dbUrl,user,passwd); 
            //con.setAutoCommit(false); 
            
        } catch (Exception e) 
        { 
            System.out.println("Server down, unable to make the connection.");         
        } 
        
    } 

    public void returnResultSetSelectStatement(String sql, int resultsetType, ConnectionHandler ch) 
    { 
        ch.r = null; 
        try 
        { 
            if (resultsetType == 1) 
                ch.s = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE); 
            else 
                ch.s = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 
                ch.r = ch.s.executeQuery(sql); 
        }
        catch (Exception e) { 
            ch.returnStatus = e.toString(); 
            System.out.println("Unable to execute the returnSelectStatement. " + e); 
        } 
    }
    
    public void updateStatement(String sql, ConnectionHandler ch) 
    { 
        Integer result = new Integer(0); 
        String message = null; 
        if (sql.trim().substring(0,1).equalsIgnoreCase("U")) message = "Number of rows updated: "; 
        else if (sql.trim().substring(0,1).equalsIgnoreCase("I")) message = "Number of rows inserted: "; 
        else message = " "; 
        try 
        { 
            ch.s = con.createStatement(); 
            result = new Integer(ch.s.executeUpdate(sql)); 
            ch.returnStatus = ""+result; 
        } catch (Exception e)  {
            System.out.println("Unable to execute the insert/update/delete statement. " + e); 
            ch.returnStatus = "Error:" + e.toString(); 
        } 
    }
    
    public boolean closeDatabase() 
    { 
        try 
        { 
            con.close(); 
            return(true); 
        } catch (Exception e) 
        { 
            System.out.println("Unable to close the database. " + e); 
            return(false); 
        } 
    } 
 
    public boolean isCloseDatabase() 
    {  
        boolean result = true;
        try
        { 
            if (con.isClosed()) result = true; 
            else result = false; 
        } catch (Exception e) 
        { 
            System.out.println("Unable to make the connection. " + e); 
        } 
        return (result); 
    } 

}
